package com.winit.config.web;

import com.winit.common.base.BaseController;
import com.winit.common.util.Constants;
import com.winit.config.manager.ConfigManager;
import com.winit.config.model.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by wangjian on 2017/9/28.
 */
@RestController
@Api(value = "config", tags = {"config"}, description = "config")
@RequestMapping(value = "/api/v1/config")
public class ConfigController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private ConfigManager configManager;

    @RequestMapping(value = "/getAppConfigs", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取app配置（页面展示用）")
    @ResponseBody
    public Map<?, ?> getAppConfigs(@ApiParam(value = "配置ID", required = false) @RequestParam(name = "config_id", required = false) String config_id,
                                   @ApiParam(value = "对返回内容的约束，0-带回master,1-不回带master,default:0", required = false) @RequestParam(name = "content_level", required = false, defaultValue = "0") String content_level,
                                   @ApiParam(value = "app_tag", required = false) @RequestParam(name = "config_app_tag", required = false) String config_app_tag,
                                   @ApiParam(value = "环境tag", required = false) @RequestParam(name = "config_env_tag", required = false) String config_env_tag,
                                   @ApiParam(value = "区域tag", required = false) @RequestParam(name = "config_zone_tag", required = false) String config_zone_tag) {
        logger.info("com.winit.config.web.ConfigController.getAppConfigs:config_id=" + config_id + ", config_app_tag=" + config_app_tag + ", config_env_tag=" + config_env_tag + ", config_zone_tag=" + config_zone_tag + ",content_level=" + content_level);
        String code = Constants.RTN_SUCCESS_CODE;
        String msg = "请求成功";

        /** 参数校验 **/
        if (config_id == null && config_app_tag == null) {
            return this.getRtn(Constants.RTN_ERROR_CODE, "config_id 与config_app_tag不能全为空 ");
        }
        if (StringUtils.isEmpty(content_level)){    //默认带回master版本
            content_level = "0";
        }
        List<Config> configList = null;
        try{
            configList = this.configManager.getAppConfigs(config_id, config_app_tag, config_env_tag, config_zone_tag, content_level);
        }catch (Exception e){
            code = Constants.RTN_ERROR_CODE;
            msg = "系统异常";
            logger.error("getAppConfigs", e);
        }
        return this.getRtn(code, msg, configList);
    }

    @RequestMapping(value = "/getAppList", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取app列表(master类型)")
    public
    @ResponseBody
    Map getAppList(@ApiParam(value = "应用类型,app tag", required = false) @RequestParam(value = "appTag", required = false) String appTag,
                   HttpServletRequest request, HttpServletResponse response) {
        logger.info("com.winit.config.web.ConfigController.getAppList: appTag=" + appTag);
        Map map = getRtn(Constants.RTN_SUCCESS_CODE, "", configManager.getAppList(appTag));
        logger.info("com.winit.config.web.ConfigController.getAppList: appTag=" + appTag);
        return map;
    }

    @RequestMapping(value = "/getBootConfForApp", method = RequestMethod.GET)
    @ApiOperation(value = "应用启动时获取配置")
    @ResponseBody
    public Map getBootConfForApp(@ApiParam(value = "app_tag", required = true) @RequestParam(name = "config_app_tag", required = true) String config_app_tag,
                                 @ApiParam(value = "环境tag", required = true) @RequestParam(name = "config_env_tag", required = true) String config_env_tag,
                                 @ApiParam(value = "区域tag", required = true) @RequestParam(name = "config_zone_tag", required = true) String config_zone_tag){
        logger.info("com.winit.config.web.ConfigController.getBootConfForApp: config_app_tag=" + config_app_tag + ", config_env_tag=" + config_env_tag + ", config_zone_tag=" + config_zone_tag);
        String code = Constants.RTN_SUCCESS_CODE;
        String msg = "success";
        Map<String, Object> rs = null;
        try {
            rs = this.configManager.getBootConfForApp(config_app_tag, config_env_tag, config_zone_tag);
        } catch (Exception e) {
            code = Constants.RTN_ERROR_CODE;
            msg = "failure";
            logger.error("getBootConfForApp", e);
        }
        return this.getRtn(code, msg, rs);
    }

}
